﻿namespace JunFan.UnitTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Domain;
    using Repositories;

    public class ItemRepositoryMock : IRepository<Item>
    {
        private readonly Dictionary<int, Item> _items = new Dictionary<int, Item>();
        private int _currentId;

        public Item Create(Item item)
        {
            item.InternalId = UniqueId();

            _items[item.InternalId] = item;
            
            return item;
        }

        public bool Exists(Item item)
        {
            return _items.Values.Any(x => x.Id == item.Id);
        }

        public Item Read(int id)
        {
            Item item;

            _items.TryGetValue(id, out item);

            return item;
        }

        public void Update(Item item)
        {
            _items[item.InternalId] = item;
        }

        public IEnumerable<Item> ReadAll()
        {
            return _items.Values;
        }

        public  IEnumerable<Item> ReadAll(Func<Item, bool> filter)
        {
            return _items.Values.Where(filter);
        }

        # region Non repository methods
        private int UniqueId()
        {
            return ++_currentId;
        }
        # endregion Non repository methods
    }
}